/*
 * Results_Panel.java
 *
 * Created on Nov 24, 2010, 12:46:40 AM
 */

package com.nortnacs;
/**
 *
 * @author Angela
 */
import java.awt.*;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Results_Panel extends JFrame {
    /** Creates new form Results_Panel */
    public Results_Panel(Form forms[]) throws IOException {
        System.out.println("This is form " + forms[0]);
        //initialize results object to retrieve data and pass in forms array
        //here we outsource the algorithm work to a Results object
        Results results = new Results(forms);

        //calls netbeans generated components, which in this case is just the frame (group layout)
        initComponents();

        //upon creation of this Results Panel object initiate creation of labels
        //based on results object created above
        this.createLabels(results);
    }

    /**Function to create labels to display percentage results*/
    private void createLabels(Results results) {
        //setup some variables
        int row;
        int column;
        int totalRows;
        int totalColumns;
        String answer;
        String header;
        /**Change font displayed on panel*/
        Font font1 = new Font("Book Antiqua", Font.PLAIN, 16);

        //begin creation of labels
        totalRows = results.getRows();
        totalColumns = results.getColumns();
        getContentPane().setLayout(new GridLayout((totalRows + 1), (totalColumns + 1)));

        /**Create Headers for panel*/
        for (row = 0; row < (totalColumns + 1); row++) {
            if (row == 0) {
                JLabel j = new JLabel();
                j.setPreferredSize(new Dimension(100, 30));
                header = ("        ");
                j.setText(header);
                this.getContentPane().add(j);
            } else {
                JLabel j = new JLabel();
                j.setPreferredSize(new Dimension(100, 30));
                header = ("Answer " + row);
                j.setFont(font1);
                j.setText(header);
                j.setHorizontalAlignment(SwingConstants.CENTER);
                this.getContentPane().add(j);
            }
        }
        /**Fill table with percentage data*/
        DecimalFormat df = new DecimalFormat("##");
        for (row = 0; row < totalRows; row++) {
            JLabel k = new JLabel();
            k.setSize(new Dimension(100, 30));
            header = ("Question " + (row + 1));
            k.setFont(font1);
            k.setText(header);
            k.setHorizontalAlignment(SwingConstants.CENTER);
            this.getContentPane().add(k);
            for (column = 0; column < totalColumns; column++) {
                JLabel j = new JLabel();
                j.setSize(new Dimension(100, 30));

                answer = (df.format(results.getPercentage(row, column)) + "%");
                j.setHorizontalAlignment(SwingConstants.CENTER);
                j.setFont(font1);
                j.setText(answer);
                this.getContentPane().add(j);
            }
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.nortnacs.NortnacsApp.class).getContext().getResourceMap(Results_Panel.class);
        setTitle(resourceMap.getString("results.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(500, 500));
        setName("results"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}